var Hogan = require('hogan.js');
var config={
	localHost:""
};
var _mm = {
	localhost:'http://webservice.carwins.com/saaswebapi/',
	//网络请求
	request: function(param) {
		return $.ajax({
			url: param.url || '',
			type: param.type || 'get',
			crossDomain: true,
			dataType: "JSON",
			contentType: "application/json; charset=utf-8",
			data: param.data || ''
		}).fail(function(error){
			console.log(error.statusText);
		});
	},
	//获取服务器地址
	getServerUrl:function(path){
		return config.localHost+path;
	},
    // 获取url参数
    getUrlParam : function(name){
        var reg     = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
        var result  = window.location.search.substr(1).match(reg);
        return result ? decodeURIComponent(result[2]) : null;
    },
    // 渲染html模板
    renderHtml : function(htmlTemplate, data){
        var template    = Hogan.compile(htmlTemplate),
            result      = template.render(data);
        return result;
    },
    successTips:function(msg){
    	alert(msg || '操作成功');
    },
    errorTips:function(){
    	alert(msg || '操作失败');
    },
    // 字段的验证，支持非空、手机、邮箱的判断
    validate : function(value, type){
        var value = $.trim(value);
        // 非空验证
        if('require' === type){
            return !!value;
        }
        // 手机号验证
        if('phone' === type){
            return /^1\d{10}$/.test(value);
        }
        // 邮箱格式验证
        if('email' === type){
            return /^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$/.test(value);
        }
    },
	//统一登录
	loginIn: function() {
		window.location.href = "./login.html?redirect=" + encodeURIComponent(window.location.href);
	},
	goHome:function(){
		window.location.href="./index.html";
	}
}

module.exports = _mm;